package br.com.hsp.system.beans;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import br.com.hsp.organizacao.Consulta;
import br.com.hsp.organizacao.Instituicao;
import br.com.hsp.system.repository.ConsultaRepository;
import br.com.hsp.system.repository.InstituicaoRepository;
import br.com.hsp.users.Paciente;



@ManagedBean
@ViewScoped
public class LogMedCBean {

	private Long medId = Long.parseLong("3");

	private Consulta consulta = new Consulta();
	
	public LogMedCBean() {
		this.consulta.setPaciente(new Paciente());
		
	}

	/*
	 * public void verificaTipoUser() { EntityManager manager =
	 * this.getManager(); ConsultaRepository consultaRepository = new
	 * ConsultaRepository(manager);
	 * 
	 * if(this.consultaId != null) { Consulta consulta =
	 * consultaRepository.procura(this.consultaId); this.setConsulta(consulta);
	 * }
	 * 
	 * }
	 */

	public void add() {
		if (this.consulta.getId() != null && this.consulta.getId() != 0) {
			EntityManager manager = this.getManager();
			ConsultaRepository consultaRepository = new ConsultaRepository(
					manager);
			System.out.println("entrou add");
			
			// this.setConsulta(consultaRepository.procura(this.consulta.getId()));
			consultaRepository.atualiza(consulta);
		}
		consulta = new Consulta();
		this.consulta.setPaciente(new Paciente());
	}

	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ConsultaRepository repository = new ConsultaRepository(manager);
		this.consulta = repository.procura(id);
	}

	public List<Instituicao> getInstituicoes() {
		List<Instituicao> instituicoes;
		EntityManager manager = this.getManager();
		InstituicaoRepository instituicaoRepository = new InstituicaoRepository(
				manager);
		instituicoes = instituicaoRepository.getClinica("HC");
		// this.instituicao = instituicoes.get(0);
		return instituicoes;
	}

	public List<Consulta> getConsultas() {
		List<Consulta> consultas;
		EntityManager manager = this.getManager();
		ConsultaRepository consultaRepository = new ConsultaRepository(manager);
		consultas = consultaRepository.getConsultasMedC(medId);
		return consultaRepository.getConsultasMedC(medId);
	}

	public Consulta getConsulta() {
		return consulta;
	}

	public void setConsulta(Consulta consulta) {
		this.consulta = consulta;
	}

}
